--云收空界
local yunshoukongjie = fk.CreateSkill {
    name = "mcdh_yunshoukongjie",
    tags = {Skill.Switch}
}

local U = require "packages/utility/utility"
local lib = require "packages/mcdh/lib"

yunshoukongjie:addEffect("viewas",{
    pattern = ".",
    prompt = function(self,player)
        if player:getSwitchSkillState(yunshoukongjie.name, false) == fk.SwitchYang then
            return "#yunshoukongjie-yang"
        else
            return "#yunshoukongjie-yin"
        end
    end,
    interaction = function(self,player)
        local names = {}
        if player:getSwitchSkillState(yunshoukongjie.name, false) == fk.SwitchYang then
            local to_use = Fk:cloneCard("fire_attack")
            if ((Fk.currentResponsePattern == nil and player:canUse(to_use)) or
                    (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
                table.insertIfNeed(names, to_use.name)
            end
        else
            for _, id in ipairs(Fk:getAllCardIds()) do
                local card = Fk:getCardById(id)
                if (card.type == Card.TypeBasic or card:isCommonTrick()) and not card.is_derived then
                    local to_use = Fk:cloneCard(card.name)
                    if ((Fk.currentResponsePattern == nil and player:canUse(to_use)) or
                            (Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use))) then
                        table.insertIfNeed(names, card.name)
                    end
                end
            end
        end
        if #names == 0 then return end
        return  U.CardNameBox { choices = names}
    end,
    card_filter = function(self, player,to_select, selected)
        return #selected == 0
    end,
    view_as = function(self, player,cards)
        local card
        if  #cards == 1 then
            card = Fk:cloneCard(self.interaction.data)
            card:addSubcard(cards[1])
        else
            return
        end
        card.skillName = yunshoukongjie.name
        return card
    end,
    enabled_at_response = function(self, player, response)
        if not response and not player:isNude() and Fk.currentResponsePattern then
            local names = {}
            if player:getSwitchSkillState(yunshoukongjie.name, false) == fk.SwitchYang then
                local to_use = Fk:cloneCard("fire_attack")
                if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use) then
                    table.insertIfNeed(names, to_use.name)
                end
            else
                for _, id in ipairs(Fk:getAllCardIds()) do
                    local card = Fk:getCardById(id)
                    if (card.type == Card.TypeBasic or card:isCommonTrick()) and not card.is_derived then
                        local to_use = Fk:cloneCard(card.name)
                        if Fk.currentResponsePattern and Exppattern:Parse(Fk.currentResponsePattern):match(to_use) then
                            table.insertIfNeed(names, card.name)
                        end
                    end
                end
            end
            return #names > 0
        end
    end,
    before_use = function(self, player, use)
        if player:getSwitchSkillState(yunshoukongjie.name, false) == fk.SwitchYin then
            player.room:handleAddLoseSkills(player,  "mcdh_zhuoshanjinqi", nil, false)
        end
    end,
})


Fk:loadTranslationTable{
    ["mcdh_yunshoukongjie"]="空界",
    [":mcdh_yunshoukongjie"] = "『云收空界』<br>转换技，①你可以获得〖尽起〗并将一张牌当【火攻】使用；②你可以将一张牌当做一张任意基本牌或任意普通锦囊牌使用。",
    ["#yunshoukongjie-yang"]="你可以获得〖尽起〗并将一张牌当【火攻】使用;",
    ["#yunshoukongjie-yin"]="你可以将一张牌当任意基本牌或普通锦囊牌使用。",
}

return yunshoukongjie